package edi.android.tmdbBrowser;

import java.util.List;

import net.sf.jtmdb.Movie;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class TMDbBrowser extends Activity {
	@SuppressWarnings("unused")
	private static final String LOG_TAG = "TMDbBrowser";
	EditText searchText;
	List<Movie> movies;
	TMDbApp app;
	Context context;
	Gallery top10Gallery;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Request for the progress bar to be shown in the title, set content
		// view
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.main);
		setProgressBarIndeterminateVisibility(true);

		// Set version number
		// TODO: remove after alpha state
		// TODO: update at each alpha release!
		TextView version = (TextView) findViewById(R.id.mainVersion);
		version.setText("0.0.alpha3");

		// set up things
		app = (TMDbApp) this.getApplication();
		context = app.getApplicationContext();
		top10Gallery = (Gallery) findViewById(R.id.mainGallery);
		Button searchMovieButton = (Button) findViewById(R.id.mainSearchMovie);
		searchText = (EditText) findViewById(R.id.mainSearchText);

		// download the top 10 in background
		TMDbApp.movieDownloader.getTop10(stopProgressHandler);

		// add event handlers
		// TODO: beautify :)
		top10Gallery.setOnItemClickListener(new OnItemClickListener() {
			// when clicking on a top10 movie, show the details of the movie
			@SuppressWarnings("unchecked")
			public void onItemClick(AdapterView parent, View v, int position,
					long id) {
				Intent intent = new Intent(context, ShowMovieActivity.class);
				intent.putExtra("movieID", (int) id);
				startActivity(intent);
			}
		});

		searchMovieButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				String searchString = searchText.getText().toString();
				if (searchString.length() < 1) {
					Toast.makeText(context,
							getString(R.string.mainEmptySearch),
							Toast.LENGTH_SHORT).show();
					return;
				}
				ProgressDialog dialog = new ProgressDialog(TMDbBrowser.this);
				dialog.setIcon(R.drawable.icon);
				dialog.setTitle(getString(R.string.mainPleaseWait));
				dialog.setMessage(getString(R.string.mainDownloading));
				dialog.show();
				TMDbApp.movieDownloader.searchMovie(searchString);
				dialog.dismiss();
				if (TMDbApp.movieDownloader.getLastMovieResult() != null) {
					Intent intent = new Intent(context, MovieListActivity.class);
					intent.putExtra("searchString", searchString);
					startActivity(intent);
				} else {
					Toast.makeText(context, getString(R.string.mainNoResult),
							Toast.LENGTH_SHORT).show();
				}
			}

		});

	}

	// callback handler to stop progress bar
	Handler stopProgressHandler = new Handler() {
		public void handleMessage(Message m) {
			setProgressBarIndeterminateVisibility(false);
			switch (m.arg1) {
			case (MovieDownloader.STATUS_IO_ERROR): {
				Toast.makeText(context, getString(R.string.mainIoError),
						Toast.LENGTH_LONG).show();
				break;
			}
			case (MovieDownloader.STATUS_PARSE_ERROR): {
				Toast.makeText(context, getString(R.string.mainParseError),
						Toast.LENGTH_LONG).show();
				break;
			}
			case (MovieDownloader.STATUS_OK): {
				if (top10Gallery != null) {
					top10Gallery.setAdapter(new MovieGalleryAdapter(context,
							TMDbApp.movieDownloader.getTop10list()));
				}
				break;
			}
			}

		}
	};
}